package com.zpy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpy.pojo.Book;
import com.zpy.pojo.Borrow;
import com.zpy.pojo.Circulate;
import com.zpy.service.BookService;
import com.zpy.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private BookService bookService;
    //本单位借出书籍的申请
    @RequestMapping("listOutput")
    public String listOutput(HttpSession session, @RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                             @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize, Borrow borrow,
                             Model model){
        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        String unit = (String) session.getAttribute("unit");//当前登录的单位
        QueryWrapper<Borrow>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bunit",unit);
        if (borrow.getStatus()!=null){
            queryWrapper.eq("status",borrow.getStatus());
        }
        List<Borrow> list1 = borrowService.list(queryWrapper);
        PageInfo<Borrow>pageInfo=new PageInfo<>(list1);
        model.addAttribute("pageInfo",pageInfo);
        return "worker-borrow-output";
    }


    //同意借出
    @RequestMapping("agree/{id}")
    public String agree(@PathVariable Integer id){
        Borrow borrow = borrowService.getById(id);
        borrow.setBorrowTime(new Date());
        borrow.setStatus(1);
        borrowService.updateById(borrow);
        //修改图书的状态为再借中
        Book book = bookService.getById(borrow.getBid());
        book.setStatus(1);
        bookService.updateById(book);
        return "redirect:/borrow/listOutput";
    }

    //拒绝借出
    @RequestMapping("refuse/{id}")
    public String refuse(@PathVariable Integer id){
        Borrow borrow = borrowService.getById(id);
        borrow.setStatus(2);//状态为已完成
        borrowService.updateById(borrow);
        return "redirect:/borrow/listOutput";
    }

    //确认已返还
    @RequestMapping("confirmReturn/{id}")
    public String confirmReturn(@PathVariable Integer id){
        Borrow borrow = borrowService.getById(id);
        borrow.setReturnTime(new Date());
        borrow.setStatus(2);//修改状态为已完成
        borrowService.updateById(borrow);
        //修改图书的状态为可借阅
        Book book = bookService.getById(borrow.getBid());
        book.setStatus(0);
        bookService.updateById(book);
        return "redirect:/borrow/listOutput";
    }

}
